package cn.itsource.org.controller;

import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.util.ExcelUtil;
import cn.itsource.basic.vo.ShopVo;
import cn.itsource.org.domain.ShopAuditLog;
import cn.itsource.org.service.IShopService;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.query.ShopQuery;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import org.apache.catalina.connector.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;

    @GetMapping("/echarts")
    public Map<String,ArrayList<Integer>> loadStateStatistics(){
        List<ShopVo> shopVos = shopService.loadStateStatistics();
        ArrayList<Integer> states = new ArrayList<>();
        ArrayList<Integer> nums = new ArrayList<>();

        for (ShopVo shopVo : shopVos) {
            states.add(shopVo.getState());
            nums.add(shopVo.getNum());
        }

        HashMap<String, ArrayList<Integer>> result = new HashMap<>();
        result.put("x",states);
        result.put("y",nums);
        return result;

    }
    @PostMapping("/import/excel")
    public void importData(@RequestPart MultipartFile file){
//        MultipartFile file, Integer titleRows(标题占多少行), Integer headerRows(表头占多少行), Class<T> pojoClass（字节码对象）
        List<Shop> shops = ExcelUtil.importExcel(file, 1, 1, Shop.class);
        shops.forEach(System.out::println);
    }

    /*excel导出*/
    @GetMapping("/export/excel")
    public void exportData(HttpServletResponse response){
        List<Shop> shops = shopService.queryAll();
       /* List<?> list, String title(标题), String sheetName（sheet页名字）, Class<?> pojoClass（List中元素的字节码对象）,
                String fileName(文件名字), boolean isCreateHeader（是否创建表头）, HttpServletResponse response*/
        ExcelUtil.exportExcel(shops,"店铺信息","店铺sheet1",Shop.class,"店铺信息.xlsx", true,response);
    }

    /*店铺审核通过接口*/
//    /shop/audit/pass
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog log){

        try {
            shopService.pass(log);
            return AjaxResult.me();
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("审核通过操作失败"+e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("审核通过操作失败！"+e.getMessage());
        }
    }

    /*店铺审核驳回接口*/
//    /shop/audit/reject
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog log){

        try {
            shopService.reject(log);
            return AjaxResult.me();
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("审核驳回操作失败"+e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("审核驳回操作失败！"+e.getMessage());
        }
    }

    /*
    * 店铺入驻接口
    * */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        
        try {
            System.out.println(shop);
            shopService.settlement(shop);
            return AjaxResult.me();
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.queryById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.queryAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> queryPage(@RequestBody ShopQuery query)
    {
        return shopService.queryPage(query);
    }
}
